package com.zhanyf.heilan.cg.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zhanyf.heilan.cg.domain.Contract;
import com.zhanyf.heilan.cg.service.ContractService;
import com.zhanyf.heilan.cg.util.ConstantUtil;

@Controller
@RequestMapping("/contract")
public class ContractController {

	@Autowired
	private ContractService contractService;

	/**
	 * 合同管理页面
	 * 
	 * @return
	 */
	@RequestMapping("/view")
	public String view() {
		return "contract/view";
	}

	/**
	 * 添加合同页面
	 * 
	 * @return
	 */
	@RequestMapping("/addContractBrieflyView")
	public String addContractBrieflyView() {
		return "contract/addContractBrieflyView";
	}

	/**
	 * 修改合同页面
	 * 
	 * @return
	 */
	@RequestMapping("/modifyContractBrieflyView")
	public ModelAndView modifyContractBrieflyView(Long id, Map<String, Object> model) {
		model.put("id", id);
		return new ModelAndView("contract/modifyContractBrieflyView", model);
	}

	/**
	 * 合同进度追踪
	 * 
	 * @return
	 */
	@RequestMapping("/processView")
	public String processView() {
		return "contract/processView";
	}

	@RequestMapping("/listContractsBriefly")
	@ResponseBody
	public Map<String, Object> listContractsBriefly(@RequestParam(value = "start") long start, @RequestParam(value = "limit") int limit) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(ConstantUtil.DATA, contractService.listContractsBriefly(start, limit));
		return map;
	}

	/**
	 * 添加合同动作
	 * 
	 * @param contract
	 * @return
	 */
	@RequestMapping("/addContractBriefly")
	@ResponseBody
	public Map<String, Object> addContractBriefly(Contract contract) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			contractService.addContractBriefly(contract);
			map.put(ConstantUtil.SUCCESS, true);
		} catch (Exception e) {
			map.put(ConstantUtil.SUCCESS, false);
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 删除合同
	 * 
	 * @param contract
	 * @return
	 */
	@RequestMapping("/deleteContractBriefly")
	@ResponseBody
	public Map<String, Object> deleteContractBriefly(Long id) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			contractService.deleteContractBriefly(id);
			map.put(ConstantUtil.SUCCESS, true);
		} catch (Exception e) {
			map.put(ConstantUtil.SUCCESS, false);
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 获取合同信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/getContractBriefly")
	@ResponseBody
	public Map<String, Object> getContractBriefly(Long id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(ConstantUtil.DATA, contractService.getContractBriefly(id));
		map.put(ConstantUtil.SUCCESS, true);
		return map;
	}

	/**
	 * 获取合同信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/modifyContractBriefly")
	@ResponseBody
	public Map<String, Object> modifyContractBriefly(Contract contract) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			contractService.modifyContractBriefly(contract);
			map.put(ConstantUtil.SUCCESS, true);
		} catch (Exception e) {
			map.put(ConstantUtil.SUCCESS, false);
			e.printStackTrace();
		}
		return map;
	}
}
